package com.net.oil.gaode;
/*
 * Created by lch on 2018/3/4 0004.
 */

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.net.oil.app.MyApp;
import com.net.oil.util.SharedPreferencesKey;
import com.net.oil.util.SharedPreferencesUtil;
import com.net.oil.util.StringUtil;

/**
 * 定位实现类
 */
public class LocationHandle {

    private static LocationHandle instance = null;

    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    private AMapLocation lastLocation = null;
    private AMapLocation currentLocation = null;

    public static LocationHandle getInstance(){
        if (instance == null){
            synchronized (LocationHandle.class){
                if (instance == null){
                    instance = new LocationHandle();
                }
            }
        }
        return instance;
    }

    /**
     * 获取最后一次定位
     * @return  AMapLocation
     */
    public AMapLocation getLastLocation(){
        if (locationClient != null){
            lastLocation = locationClient.getLastKnownLocation();
        }
        return lastLocation;
    }

    /**
     * 获取当前的位置信息
     * @return  AMapLocation
     */
    public AMapLocation getCurrentLocation(){
        return currentLocation;
    }

    public void initLocation(Context context){
        //初始化client
        locationClient = new AMapLocationClient(context);
        locationOption = getDefaultOption();
        //设置定位参数
        locationClient.setLocationOption(locationOption);
        // 设置定位监听
        locationClient.setLocationListener(locationListener);
    }

    /**
     * 开始定位
     */
    public void startLocation(){
        if (locationClient == null){
            initLocation(MyApp.getApplication());
        }
        // 启动定位
        locationClient.startLocation();
    }

    /**
     * 停止定位
     */
    public void stopLocation(){
        if (locationClient !=  null){
            // 停止定位
            locationClient.stopLocation();
        }
    }

    /**
     * 销毁定位
     * 如果AMapLocationClient是在当前Activity实例化的，
     * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
     */
    public void destroyLocation(){
        if (null != locationClient) {
            locationClient.onDestroy();
            locationClient = null;
            locationOption = null;
        }
    }

    /**
     * 获取默认定位参数
     * @return  AMapLocationClientOption
     */
    private AMapLocationClientOption getDefaultOption(){
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(10000);//可选，设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(true);//可选，设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mOption.setLocationCacheEnable(true); //可选，设置是否使用缓存定位，默认为true
        return mOption;
    }

    private AMapLocationListener locationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            currentLocation = aMapLocation;
            if (!StringUtil.isEmpty(currentLocation.getCityCode())){
                //当前定位的城市的cityCode，在公交路径规划是需要用到，因为有时获取的定位信息的cityCode为空，所以在定位成功中保存最近一次定位成功的cityCode
                SharedPreferencesUtil.setStringParam(MyApp.getApplication().getApplicationContext(), SharedPreferencesKey.CITY_CODE, currentLocation.getCityCode());
            }
        }
    };

}
